package com.bluecat.compenent;
 
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;
 
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bluecat.mapper.GenMapper;
import com.bluecat.utils.GenUtils;
 
 
 
@Component
public class GenComp {
 
	@Autowired
	GenMapper genMapper;
 
	/**
	 * 生成所有表
	 * @return
	 */
	public byte[] generatorCode() {
		 List<String> tableNames=genMapper.queryAllTable();
		 return generatorCode(tableNames);
	}
	/**
	 * 生成指定表
	 * @param tableNames
	 * @return
	 */
	public byte[] generatorCode( List<String> tableNames) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		for (String tableName : tableNames) {
			Map<String, Object> table = genMapper.queryTable(tableName);
			List<Map<String, Object>> columns = genMapper.queryColumns(tableName);
			GenUtils.generatorCode( table, columns, zip);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}
 
}